import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_module/commom/commom.dart';
import 'package:flutter_module/config/const.dart';
import 'package:flutter_module/widget/scroll/my_behavior.dart';
import 'package:url_launcher/url_launcher.dart';

import '../DemoLocalizations.dart';

class RootTabBar extends StatefulWidget {
  RootTabBar({this.pages, this.currentIndex = 0});

  final List pages;
  final int currentIndex;

  @override
  State<StatefulWidget> createState() => new RootTabBarState();
}

class RootTabBarState extends State<RootTabBar> {
  var pages = new List<BottomNavigationBarItem>();
  int currentIndex;
  var contents = new List<Offstage>();
  PageController pageController;

  @override
  void initState() {
    super.initState();
    currentIndex = widget.currentIndex;
    pageController = PageController(initialPage: currentIndex);
    for (int i = 0; i < widget.pages.length; i++) {
      TabBarModel model = widget.pages[i];
      pages.add(
        new BottomNavigationBarItem(
          icon: model.icon,
          activeIcon: model.selectIcon,
          title: new Text(model.title, style: new TextStyle(fontSize: 12.0)),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar bottomNavigationBar = new BottomNavigationBar(
      items: pages,
      type: BottomNavigationBarType.fixed,
      currentIndex: currentIndex,
      unselectedItemColor: mainTextColor,
      fixedColor: fixedColor,
      onTap: (int index) {
        setState(() => currentIndex = index);
        pageController.jumpToPage(currentIndex);
        Notice.send(NCOVActions.toTabBarIndex(), index);
      },
      unselectedFontSize: 18.0,
      selectedFontSize: 18.0,
      elevation: 0,
    );

    String title() {
//      if (currentIndex == 0) {
//        return '机器列表';
//      } else if (currentIndex == 1) {
//        return '个人中心';
//      }
//      print(DemoLocalizations.of(context).taskTitle);
//      print(DemoLocalizations.of(context).titleTowTitle);

      if (currentIndex == 0) {
        return DemoLocalizations.of(context).taskTitle;
      } else if (currentIndex == 1) {
        return DemoLocalizations.of(context).titleTowTitle;
      }
//      else if (currentIndex == 2) {
//        return '参数设置';
//      } else {
//        return '';
//      }
    }

    return new Scaffold(
      bottomNavigationBar: new Theme(
        data: new ThemeData(
          canvasColor: Colors.grey[50],
          highlightColor: Colors.transparent,
          splashColor: Colors.transparent,
        ),
        child: new Container(
          decoration: BoxDecoration(
              border: Border(top: BorderSide(color: lineColor, width: 0.2))),
          child: bottomNavigationBar,
        ),
      ),
      appBar: AppBar(
        iconTheme: IconThemeData(
          color: Colors.black, //修改颜色
        ),
        title: Text(
          title() ?? '',
          // style: TextStyle(color: Color(0xff000000)),
        ),
        // backgroundColor: Color(0xffD7D8DA),
        centerTitle: true,
      ),

//       new ComMomBar(
//         titleW: new AnimatedSwitcher(
//           duration: Duration(milliseconds: 100),
//           transitionBuilder: (Widget child, Animation<double> animation) {
//             var tween = Tween<double>(begin: 0, end: 1);
//             return FadeTransition(
//               child: child,
//               opacity: tween.animate(animation),
//             );
//           },
//           child: new Text(
//             title() ?? '',
//             key: ValueKey(title() ?? ''),
//             style: TextStyle(color: Colors.white, fontSize: 20.0),
//             textAlign: TextAlign.center,
//           ),
//         ),
// //        rightDMActions: <Widget>[
// //          new MaterialButton(
// //            onPressed: () => launch('https://github.com/ahyangnb/flutter_module'),
// //            padding: EdgeInsets.symmetric(horizontal: 10.0),
// //            child: new Text(
// //              '开源地址',
// //              style: TextStyle(color: Colors.white),
// //            ),
// //          )
// //        ],
//       ),
      body: new ScrollConfiguration(
        behavior: MyBehavior(),
        child: new PageView.builder(
          itemBuilder: (BuildContext context, int index) =>
              widget.pages[index].page,
          controller: pageController,
          itemCount: pages.length,
          physics: Platform.isAndroid
              ? new ClampingScrollPhysics()
              : new NeverScrollableScrollPhysics(),
          onPageChanged: (int index) {
            setState(() => currentIndex = index);
          },
        ),
      ),
    );
  }
}

class TabBarModel {
  const TabBarModel({this.title, this.page, this.icon, this.selectIcon});

  final String title;
  final Widget icon;
  final Widget selectIcon;
  final Widget page;
}
